/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Entrance.java
 *
 * Created on Sep 20, 2009, 4:20:16 PM
 */

package viewer.main;

import data.processing.Controller;
import java.io.File;
import java.io.FileFilter;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;


/**
 *
 * @author Luciana
 */
public class Entrance extends javax.swing.JFrame {
    private String strCurrentPath="";
    private String strCurrentFilePath="";
    
    /** Creates new form Entrance */
    public Entrance() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtFieldSuporte = new javax.swing.JTextField();
        radioEqual = new javax.swing.JRadioButton();
        radioPrefix = new javax.swing.JRadioButton();
        radioSuffix = new javax.swing.JRadioButton();
        radioExtremity = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        txtFSize = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        radioBtnA_C = new javax.swing.JRadioButton();
        btnBuildTree = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        txtFieldFilePath = new javax.swing.JTextField();
        jButtonFileToGSP = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtFPath = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jComboBoxStep1 = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jComboBoxStep2 = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jComboBoxStep3 = new javax.swing.JComboBox();
        this.jComboBoxStep3.setVisible(false);
        jLabel6 = new javax.swing.JLabel();
        txtMetrics = new javax.swing.JTextField();
        btnExecutePreProcessing = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaMessages = new javax.swing.JTextArea();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListFiles = new javax.swing.JList();
        btnDeleteFiles = new javax.swing.JButton();

        buttonGroup.add(radioEqual);
        buttonGroup.add(radioPrefix);
        buttonGroup.add(radioSuffix);
        buttonGroup.add(radioExtremity);
        buttonGroup.add(radioBtnA_C);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(" Sequential Patterns Viewer");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(715, 569));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Informações para a execução do algoritmo GSP ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", 0, 11))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel2.setText("Suporte:");

        txtFieldSuporte.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));

        radioEqual.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        radioEqual.setText("Métodos iguais: abc");

        radioPrefix.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        radioPrefix.setText("Prefixo: a%, ab%");

        radioSuffix.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        radioSuffix.setText("Sufixo: %c, %bc");

        radioExtremity.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        radioExtremity.setText("Qualquer motivo");

        jLabel3.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel3.setText("Tamanho a ser considerado:");

        txtFSize.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));

        jLabel4.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel4.setText("Tamanho padrão 1. Ex. de prefixos: tamanho 1 = a%, tamanho 2 = ab%");

        jLabel5.setText("Suporte padrão: 5%");

        radioBtnA_C.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        radioBtnA_C.setText("Padrões: a%c");

        btnBuildTree.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        btnBuildTree.setText("Minerar Sequencias");
        btnBuildTree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuildTreeActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel10.setText("Arquivo de rastro para mineração: ");

        txtFieldFilePath.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));

        jButtonFileToGSP.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11));
        jButtonFileToGSP.setText("...");
        jButtonFileToGSP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFileToGSPActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFieldFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonFileToGSP))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(radioExtremity)
                                .addComponent(radioBtnA_C)
                                .addComponent(radioSuffix)
                                .addComponent(radioPrefix)
                                .addComponent(radioEqual)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtFieldSuporte, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(31, 31, 31)
                                    .addComponent(jLabel5))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtFSize, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(29, 29, 29)
                                    .addComponent(jLabel4)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(btnBuildTree, javax.swing.GroupLayout.Alignment.TRAILING)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFieldFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonFileToGSP)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtFieldSuporte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(31, 31, 31)
                .addComponent(radioEqual)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(radioPrefix)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(radioSuffix)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(radioBtnA_C)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(radioExtremity)
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtFSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(17, 17, 17)
                .addComponent(btnBuildTree)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Pré-processamento dos rastros ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", 0, 11))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel1.setText("Caminho dos rastros: ");

        txtFPath.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));

        jButton1.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11));
        jButton1.setText("...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Opções de tratamento dos dados ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", 0, 11))); // NOI18N
        jLabel9.setVisible(false);

        jLabel7.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel7.setText("Passo 1:");

        jComboBoxStep1.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        jComboBoxStep1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Compressor", "Metrics" }));

        jLabel8.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel8.setText("Passo 2:");

        jComboBoxStep2.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        jComboBoxStep2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nenhum", "Metrics", "Compressor" }));
        jComboBoxStep2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxStep2ItemStateChanged(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel9.setText("Passo 3:");

        jComboBoxStep3.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        jComboBoxStep3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nenhum", "Compressor", "Metrics" }));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBoxStep1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBoxStep2, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBoxStep3, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jComboBoxStep1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxStep2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jComboBoxStep3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        jLabel6.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        jLabel6.setText("Número total de métodos chamados internamente: ");

        txtMetrics.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));

        btnExecutePreProcessing.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        btnExecutePreProcessing.setText("Executar");
        btnExecutePreProcessing.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExecutePreProcessingActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFPath, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addGap(64, 64, 64))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtMetrics, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(342, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(599, Short.MAX_VALUE)
                .addComponent(btnExecutePreProcessing)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtMetrics, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnExecutePreProcessing)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Saída de mensagens", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", 0, 11))); // NOI18N

        jTextAreaMessages.setColumns(20);
        jTextAreaMessages.setEditable(false);
        jTextAreaMessages.setRows(5);
        jScrollPane1.setViewportView(jTextAreaMessages);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Arquivos e diretórios gerados ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Sans Serif", 0, 11))); // NOI18N

        jListFiles.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        jScrollPane2.setViewportView(jListFiles);

        btnDeleteFiles.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11));
        btnDeleteFiles.setText("Apagar arquivos selecionados");
        btnDeleteFiles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteFilesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnDeleteFiles, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDeleteFiles)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 1155, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void insertMessages(Controller controller) {
        this.jTextAreaMessages.append(controller.getMessages().toString());
        controller.initialiseMessages();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        JFileChooser fc;
        if(strCurrentPath.compareTo("")!=0){
            fc = new JFileChooser(strCurrentPath);
        }else{
            fc = new JFileChooser();
        }
        //fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int intReturn = fc.showOpenDialog(txtFPath);
        if(intReturn == JFileChooser.APPROVE_OPTION){
            strCurrentPath=fc.getSelectedFile().getAbsolutePath();
            txtFPath.setText(strCurrentPath);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnBuildTreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuildTreeActionPerformed
        /* Getting the chosen radio */
        String requiredMethod = " - Motivo: Prefixo";
        int option = 2;//for enabled radioPrefix
        if(this.radioEqual.isSelected()){ option = 1; requiredMethod = " - Motivo: Igual";}
        else if(this.radioSuffix.isSelected()){ option = 3; requiredMethod = " - Motivo: Sufixo";}
        else if(this.radioExtremity.isSelected()) {option = 4; requiredMethod = " - Qualquer Motivo";}
        else if(this.radioBtnA_C.isSelected()) {option = 5; requiredMethod = " - Motivo: a..c";}
        /* Gets others parameters */
        int size = 1;
        double support = 0.05;
        if(!this.txtFSize.getText().equals("")) size = Integer.parseInt(this.txtFSize.getText());
        if(!this.txtFieldSuporte.getText().equals("")) support = Double.parseDouble(this.txtFieldSuporte.getText())/100;
        if(!this.strCurrentFilePath.equals("")){
            //boolean isOk = Tree.start(this.strCurrentFilePath, option, size, support, requiredMethod);
           // if(!isOk) jTextAreaMessages.append("\n>> Nenhum candidato encontrado para o suporte "+(support*100)+"%.\n");
        }
    }//GEN-LAST:event_btnBuildTreeActionPerformed

    private void btnExecutePreProcessingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExecutePreProcessingActionPerformed
        String step = "Passo 1";
        try{
        String dir = "";
        if(!strCurrentPath.startsWith("/")) dir = "\\";
        else dir = "/";
        Controller controller = new Controller(this.txtMetrics.getText(), dir);
        String msgDefault = "\n >> Insira o Número de Chamadas Internas para executar o \ncompressor através de métricas.";
        File newFilePathStep = null;
        jTextAreaMessages.setText("");
        jListFiles.setModel(new DefaultListModel());
        if((this.jComboBoxStep1.getSelectedIndex() == 1 || this.jComboBoxStep2.getSelectedIndex() == 1 || this.jComboBoxStep3.getSelectedIndex() == 2) && txtMetrics.getText().equals("")){
            jTextAreaMessages.append(msgDefault);
        }else{
//            newFilePathStep = controller.step1(this.jComboBoxStep1.getSelectedIndex(), this.strCurrentPath);
            if(newFilePathStep.list().length>1) {
                String verify = newFilePathStep.getParentFile().getAbsolutePath()+dir+"Merge"+dir+"Thread"+dir+"data.trace";
                step = "Processo de merge no passo 1";
                if(!new File(verify).exists()) newFilePathStep = mergeFiles(newFilePathStep, dir);
                else newFilePathStep = new File(verify);
            }
            insertMessages(controller);
            updateList(newFilePathStep);
            if(this.jComboBoxStep2.getSelectedIndex() > 0){
                step = "Passo 2";
//                newFilePathStep = controller.step2(this.jComboBoxStep2.getSelectedIndex(), newFilePathStep.getParentFile().getParentFile());
                updateList(newFilePathStep);
                insertMessages(controller);
                if(this.jComboBoxStep3.getSelectedIndex() > 0){
                    step = "Passo 3";
//                    newFilePathStep = controller.step3(this.jComboBoxStep3.getSelectedIndex(), newFilePathStep);
                    updateList(newFilePathStep);
                    insertMessages(controller);
                    txtFieldFilePath.setText(newFilePathStep.getAbsolutePath());
                }else txtFieldFilePath.setText(newFilePathStep.getAbsolutePath());
            }else txtFieldFilePath.setText(newFilePathStep.getAbsolutePath());
        }
        strCurrentFilePath = txtFieldFilePath.getText();
        }catch(Exception e){
            jTextAreaMessages.append("\n=============\nErro no "+step + " "+e.toString());
        }
    }//GEN-LAST:event_btnExecutePreProcessingActionPerformed

    private File mergeFiles(File filesPath, String dir){
        File[] thread_dirs = filesPath.listFiles((new FileFilter() {
			public boolean accept(File file) {
				return file.isDirectory() && file.getName().startsWith("Thread");
			}
		}));
        File file = new File(filesPath.getParentFile().getAbsolutePath()+dir+"Merge");
        file.mkdir();
        
        String th1, th2;
        StringBuffer saida = new StringBuffer();
        StringBuffer nomeArqSaida = new StringBuffer();
        nomeArqSaida.append("mergeth1_th2");
        for(int i=0; i<thread_dirs.length; i++){
            if(i==0){
                th1 = thread_dirs[i].listFiles()[0].getAbsolutePath();
                th2 = thread_dirs[i+1].listFiles()[0].getAbsolutePath();
                saida.append(file.getAbsolutePath()+dir+"mergeth1_th2.txt");
//                merge = new MergeThreads(th1, th2, saida.toString());
//                merge.combinarThreads();
            } else if((i+1)>2){
                th1 = file.getAbsolutePath()+dir+nomeArqSaida.toString()+".txt";
                th2 = thread_dirs[i].listFiles()[0].getAbsolutePath();
                nomeArqSaida.append("_th"+(i+1));
                saida = new StringBuffer();
                saida.append(file.getAbsolutePath()+dir+nomeArqSaida+".txt");
//                merge = new MergeThreads(th1, th2, saida.toString());
//                merge.combinarThreads();
                File removeFile = new File(th1);
                removeFile.delete();
               }
          
        }
        File mergedFile = new File(saida.toString());
        File outDir = new File(file.getAbsolutePath()+dir+"Thread");
        outDir.mkdir();
        mergedFile.renameTo(new File(outDir+dir+"data.trace"));
        return new File(outDir.getAbsolutePath()+dir+"data.trace");
    }

    private void updateList(File newFilePathStep) {
        DefaultListModel listModel = new DefaultListModel();
        listModel.addElement(newFilePathStep.getAbsolutePath());
        if(jListFiles.getModel().getSize() == 0) jListFiles.setModel(listModel);
        else{
            DefaultListModel listModelArray = (DefaultListModel)jListFiles.getModel();
            if(!listModelArray.contains(listModel)) listModelArray.addElement(newFilePathStep.getAbsolutePath());
            jListFiles.setModel(listModelArray);
        }
    }

    private void jComboBoxStep2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxStep2ItemStateChanged
       if(this.jComboBoxStep2.getSelectedIndex() > 0){
           this.jComboBoxStep3.setVisible(true);
           jLabel9.setVisible(true);
       }
       else{
           this.jComboBoxStep3.setVisible(false);
           jLabel9.setVisible(false);
       }
    }//GEN-LAST:event_jComboBoxStep2ItemStateChanged

    private void jButtonFileToGSPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFileToGSPActionPerformed
        JFileChooser fc;
        if(strCurrentFilePath.compareTo("")!=0){
            fc = new JFileChooser(strCurrentFilePath);
        }else{
            fc = new JFileChooser();
        }
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int intReturn = fc.showOpenDialog(txtFPath);
        if(intReturn == JFileChooser.APPROVE_OPTION){
            strCurrentFilePath=fc.getSelectedFile().getAbsolutePath();
            txtFieldFilePath.setText(strCurrentFilePath);
        }
}//GEN-LAST:event_jButtonFileToGSPActionPerformed

    private void btnDeleteFilesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteFilesActionPerformed
        Object[] selectedFiles = jListFiles.getSelectedValues();
        for(int i=0; i<selectedFiles.length; i++){
            String file = (String)selectedFiles[i];
            File removeFile = new File(file);
            if(removeFile.exists()){
                removeFile.delete();
            }
        }
        for(int i=0; i<selectedFiles.length; i++){
            ((DefaultListModel)(jListFiles.getModel())).removeElement(selectedFiles[i]);
        }
        txtFieldFilePath.setText("");
}//GEN-LAST:event_btnDeleteFilesActionPerformed

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                Entrance frame = new Entrance();
//                frame.setLocationRelativeTo(null);
//                frame.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuildTree;
    private javax.swing.JButton btnDeleteFiles;
    private javax.swing.JButton btnExecutePreProcessing;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonFileToGSP;
    private javax.swing.JComboBox jComboBoxStep1;
    private javax.swing.JComboBox jComboBoxStep2;
    private javax.swing.JComboBox jComboBoxStep3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jListFiles;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaMessages;
    private javax.swing.JRadioButton radioBtnA_C;
    private javax.swing.JRadioButton radioEqual;
    private javax.swing.JRadioButton radioExtremity;
    private javax.swing.JRadioButton radioPrefix;
    private javax.swing.JRadioButton radioSuffix;
    private javax.swing.JTextField txtFPath;
    private javax.swing.JTextField txtFSize;
    private javax.swing.JTextField txtFieldFilePath;
    private javax.swing.JTextField txtFieldSuporte;
    private javax.swing.JTextField txtMetrics;
    // End of variables declaration//GEN-END:variables



}
